import React, { Component } from "react";

export default class Demo extends Component {
    state = {
        count: 0,
    };

    increment = () => {
        const { count } = this.state;

        // 1. 对象式setState
        // this.setState({ count: count + 1 }, () => {
        //     // 此回调函数在render()调用之后才会被调用
        //     console.log("异步获取state中的值", this.state.count);
        // });
        // console.log("同步获取state中的值", this.state.count);

        // 2. 函数式setState
        this.setState((state, props) => {
            return {
                count: count + 1,
            };
        });
    };

    render() {
        return (
            <div>
                <h2>当前求和为：{this.state.count}</h2>
                <button onClick={this.increment}>点我+1</button>
            </div>
        );
    }
}
